package com.osdp.uc.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.osdp.uc.entity.UcUser;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import static org.apache.ibatis.type.JdbcType.*;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 *
 * @Author
 * @since
 */
@Repository
//@Mapper
public interface UcUserMapper extends BaseMapper<UcUser> {
	/**
	 * <if test='entity.username != null andentity.username !='''>
	 * # where id in <foreach collection='ids' open='(' close=')' separator=',' item='item'>#{item}</foreach>
	 * @param entity
	 * @return
	 */
	@Update({"<script>",
			"update uc_user set " +
					"username=#{entity.username}, realname=#{entity.realname}, avatar=#{entity.avatar}," +
					"birthday=#{birthday}, sex=#{sex}, email=#{email}, phone=#{phone}," +
					"entry_date=#{entryDate}, terminate_date=#{terminateDate}," +
					"status=#{status}, update_by=#{updateBy}, update_time=#{updateTime} " +
					" where id = #{id}",
			"</script>"})
	void updateUser(UcUser entity);

	@Insert({"<script>",
			"insert into uc_user(id,username,realname,password,salt,avatar,birthday,sex,email,phone," +
					"entry_date,terminate_date,status,del_flag,create_by,create_time) values  " +
					"(#{id}, #{username}, #{realname}, #{password}, #{salt}," +
					"#{avatar},#{birthday},#{sex}, #{email}, " +
					"#{phone},#{entryDate},#{terminateDate}," +
					"#{status},#{delFlag}, #{createBy}, #{createTime})",
			"</script>"})
	void insertUser(UcUser entity);
	/**
	  * 通过用户账号查询用户信息
	 * @param id
	 * @return List<UcUser>
	 */
	@Select("select u.*, ur.role_id from  uc_user u left join sc_role_user ur on u.id = ur.user_id where id = #{id}")
	UcUser getUserById(@Param("id") String id);


	/**
	 *  根据部门Id查询用户信息
	 * @param page
	 * @param orgId
	 * @return
	 */
	@Select({"<script>",
			"select distinct u.id,u.user_name,u.real_name,u.avatar,u.birthday,u.gender,u.email,u.phone," +
					"u.validation,u.priority_depart_id,u.entry_date,u.leave_date " +
					"from uc_user u left join uc_user_position upos on u.id= upos.user_id " +
//			"left join uc_position pos on upos.position_id = pos.id " +
					"where 1=1 " +
					"<if test=\"orgId !=null and orgId !=''\">" +
					"and upos.org_id= #{orgId} " +
					"</if>" +
					"<if test=\"user.userName !=null and user.userName !=''\">" +
					" and u.user_name like concat('%',#{user.userName},'%') " +
					"</if>" +
					"<if test=\"user.realName !=null and user.realName !=''\">" +
					" and u.real_name like concat('%',#{user.realName},'%') " +
					"</if>" +
					"<if test=\"user.phone !=null and user.phone !=''\">" +
					"and u.phone like concat('%',#{user.phone},'%') " +
					"</if>" +
					"<if test=\"user.gender !=null and user.gender !=''\">" +
					"and u.gender =#{user.gender} " +
					"</if>" +
					"order by create_time desc",
			"</script>"})
	@Results(id="UcUserMap", value= {
			@Result(column = "id", property = "id", jdbcType = VARCHAR),
			@Result(column = "user_name", property = "userName",jdbcType = VARCHAR),
			@Result(column = "real_name", property = "realName", jdbcType = VARCHAR),
			@Result(column = "pass_word", property = "passWord", jdbcType = VARCHAR),
			@Result(column = "salt", property = "salt", jdbcType = VARCHAR),
			@Result(column = "avatar", property = "avatar", jdbcType = VARCHAR),
			@Result(column = "gender", property = "gender", jdbcType = VARCHAR),
			@Result(column = "email", property = "email", jdbcType = VARCHAR),
			@Result(column = "phone", property = "phone", jdbcType = VARCHAR),
			@Result(column = "birthday", property = "birthday",jdbcType = DATE),
			@Result(column = "entry_date", property = "entryDate",jdbcType = TIMESTAMP),
			@Result(column = "leave_date", property = "leaveDate",jdbcType = TIMESTAMP),
			@Result(column = "family_addr", property = "familyAddr", jdbcType = VARCHAR),
			@Result(column = "work_tel", property = "workTel", jdbcType = VARCHAR),
			@Result(column = "work_addr", property = "workAddr", jdbcType = VARCHAR),
			@Result(column = "validation", property = "validation", jdbcType = VARCHAR),
			@Result(column = "priority_depart_id", property = "priorityDepartId", jdbcType = VARCHAR),
			@Result(column = "create_time", property = "createTime",jdbcType = TIMESTAMP),
			@Result(column = "update_time", property = "updateTime",jdbcType = TIMESTAMP),
			@Result(column = "create_by", property = "createBy", jdbcType = VARCHAR),
			@Result(column = "update_by", property = "updateBy", jdbcType = VARCHAR),
//			@Result(column = "id",property = "userLvFriend",
//					//一对一关系映射用one注解,这里查询 用户的班级信息
//					//select 属性中填写执行方法的全限定类名加方法名
//					//FetchType.EAGER 是立即加载
//					//FetchType.LAZY 是懒加载
//					one = @One(select = "com.baidu.mybatis_test_1.dao.UserLvFriendDao.getLvFriendbyUserId",fetchType = FetchType.EAGER)),
//			@Result(column = "id",property = "roleList",
//					//一对多关系映射many注解，这里查询用户的角色信息
//					many = @Many(select = "com.osdp.sc.mapper.ScRoleMapper.queryRolesByUserId",fetchType = FetchType.LAZY))
			@Result(column = "id", property = "ucUserPositionList",
			many = @Many(select = "com.osdp.uc.mapper.UcUserPositionMapper.queryUserPositionByUserId", fetchType = FetchType.LAZY))
	})
	IPage<UcUser> pageListByOrgId(@Param("orgId") String orgId, @Param("user") UcUser user, Page<UcUser> page);

	/**
	 * 根据角色Id查询用户信息
	 * @param page
	 * @param
	 * @return
	 */
//	IPage<UcUser> getUserByRoleId(Page page, @Param("roleId") String roleId, @Param("username") String username);
	
	/**
	 * 根据用户名设置部门ID
	 * @param username
	 * @param orgCode
	 */
//	@Update("UPDATE uc_user SET org_code = #{orgCode} where username = #{username}")
//	void updateUserDepart(@Param("username") String username, @Param("orgCode") String orgCode);


//	List<HashMap<String,Object>> selectMap(@Param("extend") String extend, @Param("extId") String extId);


	/**
	 * 根据角色获取人员
	 * @param ids
	 * @return
	 */
//	List<UcUser> getUserByRoles(@Param("ids") String... ids);

	/**
	 * 根据岗位获取人员
	 * @param ids
	 * @return
	 */
//	List<UcUser> getUserByPosition(@Param("ids") String... ids);


	/**
	 *根据角色code获取用户列表
	 * @param roleCodes
	 * @return
	 */
//	List<UcUser> getUserByRoleCode(@Param("roleCodes") String... roleCodes);


	/**
	 * 根据用户机构查询出用户id列表
	 * @param userId
	 * @return
	 */
//	List<String> getUserIdListByDepId(@Param("userId") String userId);


	/**
	 * 根据用户id查询用户的数据权限
	 * @param userId
	 * @return
	 */
//	List<String> getUserIdListByRoleData(@Param("userId") String userId);


	/**
	 * 查询部门以及部门子级
	 * @param model
	 * @return
	 */
//	List<UcUser> getUserByDepartAndChildren(UcUserVo model);

//	List<UcUser> getUserByUorgId(@Param("uorgId") String uorgId);

//	IPage<UcUserVo> getUserByDepartAndChildrenPage(Page page, @Param("ew") UcUserVo position);


	/**
	 * 搜索
	 * @param page
	 * @param sysUser
	 * @param ids
	 * @return
	 */
//	IPage<UserVo> getSysUsers(Page page, @Param("ew") UcUser sysUser, @Param("ids") List<String> ids);

}
